added samples
[windows-sources.git] / sdk / samples / all in on code / Visual Studio 2008 / VBSL3CustomControl / HighLightTextBlock.vb
blob2d053d83af38ac3d28205ef2f46aa0932b105dd7
1 '****************************** Module Header *******************************
2 ' Module Name: HighLightTextBlock.vb
3 ' Project: VBSL3CustomControl
4 ' Copyright (c) Microsoft Corporation.
5 '
6 ' Implementation of custom control HighLightTextBlock.
7 '
8 ' This source is subject to the Microsoft Public License.
9 ' See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
10 ' All other rights reserved.
12 ' History:
13 ' * 8/18/2009 8:06 PM Mog Liang Created
14 '****************************************************************************
16 Imports System.Windows.Threading
17 Imports System.ComponentModel
19 <TemplateVisualState(Name:="NonHighLight", GroupName:="HightLightStates"), TemplateVisualState(Name:="HighLight", GroupName:="HightLightStates")> _
20 Public Class HighLightTextBlock : Inherits Control
22 Public Sub New()
23 ' Register custom control's default style
24 MyBase.DefaultStyleKey = GetType(HighLightTextBlock)
25 Me.InitTimer()
26 End Sub
28 Private Sub Dehighlight()
29 VisualStateManager.GoToState(Me, "NonHighLight", True)
30 Me._timer.Stop()
31 End Sub
33 ' User call this method to highlight text.
34 Public Sub Highlight()
35 ' Change control state to 'HightLight'
36 VisualStateManager.GoToState(Me, "HighLight", True)
37 If Me.AutoDehighlight Then
38 ' Start timer
39 Me._timer.Start()
40 End If
41 End Sub
43 ' Initialize timer
44 Private Sub InitTimer()
45 Me._timer = New DispatcherTimer
46 Me._timer.Interval = TimeSpan.FromSeconds(1)
47 ' When time out, change HighLightStates to 'NonHighLight'
48 AddHandler Me._timer.Tick, New EventHandler(AddressOf Me.Timer_OnTick)
49 End Sub
51 Private Sub Timer_OnTick(ByVal sender As Object, ByVal e As EventArgs)
52 Dehighlight()
53 End Sub
55 ' Use this property to (de)highlight the text.
56 Private Shared Sub IsHighlightedChanged(ByVal sender As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
57 Dim newvalue As Boolean? = DirectCast(e.NewValue, Boolean?)
58 If newvalue.Value Then
59 DirectCast(sender, HighLightTextBlock).Highlight()
60 Else
61 DirectCast(sender, HighLightTextBlock).Dehighlight()
62 End If
63 DirectCast(sender, HighLightTextBlock).IsHighlighted = DirectCast(e.NewValue, Boolean?)
64 End Sub
66 Public Overrides Sub OnApplyTemplate()
67 MyBase.OnApplyTemplate()
68 End Sub
71 ' If this property is set to true, the text will automatically be dehilighted after a specific time. Use the LightTime to control the highlight period.
72 Private _AutoDehighlight As Boolean
73 Public Property AutoDehighlight() As Boolean
74 Get
75 Return _AutoDehighlight
76 End Get
77 Set(ByVal value As Boolean)
78 _AutoDehighlight = value
79 End Set
80 End Property
82 <TypeConverter(GetType(NullableBoolConverter))> _
83 Public Property IsHighlighted() As Boolean?
84 Get
85 Return DirectCast(MyBase.GetValue(HighLightTextBlock.IsHighlightedProperty), Boolean?)
86 End Get
87 Set(ByVal value As Boolean?)
88 MyBase.SetValue(HighLightTextBlock.IsHighlightedProperty, value)
89 End Set
90 End Property
93 ' Expose LightTime property for highlight time.
94 Public Property LightTime() As TimeSpan
95 Get
96 If (Me._timer Is Nothing) Then
97 Me.InitTimer()
98 End If
99 Return Me._timer.Interval
100 End Get
101 Set(ByVal value As TimeSpan)
102 If (Me._timer Is Nothing) Then
103 Me.InitTimer()
104 End If
105 Me._timer.Interval = value
106 End Set
107 End Property
109 ' In order to use templatebinding on 'Text' property,
110 ' it must be dependency property.
111 Public Property [Text]() As String
113 Return CStr(MyBase.GetValue(HighLightTextBlock.TextProperty))
114 End Get
115 Set(ByVal value As String)
116 MyBase.SetValue(HighLightTextBlock.TextProperty, value)
117 End Set
118 End Property
121 ' Fields
122 Private _timer As DispatcherTimer
123 Public Shared ReadOnly IsHighlightedProperty As DependencyProperty = DependencyProperty.Register("DependencyProperty", GetType(Boolean?), GetType(HighLightTextBlock), New PropertyMetadata(False, New PropertyChangedCallback(AddressOf HighLightTextBlock.IsHighlightedChanged)))
124 Public Shared ReadOnly TextProperty As DependencyProperty = DependencyProperty.Register("Text", GetType(String), GetType(HighLightTextBlock), Nothing)
125 End Class